package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.api.dto.UserSearchDto;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.UserSearch;
import com.qinggeng.project.system.mapper.UserSearchMapper;
import com.qinggeng.project.system.service.IUserSearchService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class UserSearchServiceImpl implements IUserSearchService {
    @Resource
    private UserSearchMapper userSearchMapper;

    @Override
    public int insertSearch(Long userId,String content) {
        UserSearch search=selectUserSearchByContent(userId,content);
        if(search!=null){//如果存在相同的搜索内容
            search.setCount(search.getCount()+1);//搜索次数加1
            if(search.getShowFlag()==20){//如果该条记录已被用户清除
                search.setShowFlag(10);//设置显示标志为10=已显示
            }
            search.setUpdateTime(DateUtils.getNowDate());//设置更新时间
            return updateSearch(search);
        }else{
            search=new UserSearch();
            search.setUserId(ApiUtils.getUserId());
            search.setContent(content);
            search.setUpdateTime(DateUtils.getNowDate());
            return userSearchMapper.insertUserSearch(search);
        }
    }

    @Override
    public int deleteSearchByUserId(Long userId) {
        return userSearchMapper.deleteUserSearchByUserId(userId);
    }

    @Override
    public int updateSearch(UserSearch userSearch) {
        return userSearchMapper.updateUserSearch(userSearch);
    }

    @Override
    public List<UserSearch> selectUserSearchList(UserSearch userSearch){
        return userSearchMapper.selectUserSearchList(userSearch);
    }

    @Override
    public List<UserSearchDto> selectUserSearchListDtoByUserId(Long userId) {
        return userSearchMapper.selectUserSearchListDtoByUserId(userId);
    }

    @Override
    public UserSearch selectUserSearchByContent(Long userId, String content){
        return userSearchMapper.selectUserSearchByContent(userId,content);
    }
}
